////////////////////////////////////////////////////////////////////////////////
// Filename: graphicsclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "graphicsclass.h"


GraphicsClass::GraphicsClass()
{
	m_D3D = 0;
	m_Camera = 0;
	m_Model = 0;
}


GraphicsClass::GraphicsClass(const GraphicsClass& other)
{
}


GraphicsClass::~GraphicsClass()
{
}


bool GraphicsClass::Initialize(int screenWidth, int screenHeight, HWND hwnd)
{
	bool result;

		
	// Create the Direct3D object.
	m_D3D = new D3DClass;
	if(!m_D3D)
	{
		return false;
	}

	// Initialize the Direct3D object.
	result = m_D3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR);
	if(!result)
	{
		return false;
	}

	// Create the camera object.
	m_Camera = new CameraClass;
	if(!m_Camera)
	{
		return false;
	}

	// Create the model object.
	m_Model = new ModelClass;
	if(!m_Model)
	{
		return false;
	}

	// Initialize the model object.
	result = m_Model->Initialize(m_D3D->GetDevice(), hwnd);
	if(!result)
	{
		return false;
	}

	m_Camera->SetPosition(0.0f, 3.0f, -10.0f);

	return true;
}


void GraphicsClass::Shutdown()
{
	// Release the model object.
	if(m_Model)
	{
		m_Model->Shutdown();
		delete m_Model;
		m_Model = 0;
	}

	// Release the camera object.
	if(m_Camera)
	{
		delete m_Camera;
		m_Camera = 0;
	}

	// Release the Direct3D object.
	if(m_D3D)
	{
		m_D3D->Shutdown();
		delete m_D3D;
		m_D3D = 0;
	}

	return;
}


void GraphicsClass::Frame()
{
	// Set the position of the camera.
	//m_Camera->SetPosition(0.0f, 3.0f, -10.0f);

	return;
}


void GraphicsClass::Render()
{
	D3DXMATRIX viewMatrix, projectionMatrix, worldMatrix, scaleMatrix;
	static float rotation = 0.0f;

	static float scaling = 2.0f;
	static float scale = 2.0f;


	// Clear the buffers to begin the scene.
	m_D3D->BeginScene();

	// Generate the view matrix based on the camera's position.
	m_Camera->Render();

	// Get the view, projection, and world matrices from the camera and d3d objects.
	m_Camera->GetViewMatrix(viewMatrix);
	m_D3D->GetProjectionMatrix(projectionMatrix);
	m_D3D->GetWorldMatrix(worldMatrix);

	D3DXMatrixIdentity(&scaleMatrix);

	// Rotate the world matrix.
	rotation += (float)D3DX_PI * 0.0001f;
	if(rotation > 360.0f)
	{
		rotation -= 360.0f;
	}

	if(static_cast<int>(scaling) % 2)
	{
		scaling += 0.01f;
		scale = 1.0f;
	}

	else
	{
		scaling += 0.01f;
		scale = 2.0f;
	}

	//D3DXMatrixRotationY(&worldMatrix, rotation);

	if(CInput::GetInstance()->GetKey(DIK_P))
	{
		D3DXMatrixScaling(&scaleMatrix, scale, scale, scale);

		worldMatrix *= scaleMatrix;
	}
	

	// Send the three matricies to the model so the shader can use them to render with.
	m_Model->SetMatrices(worldMatrix, viewMatrix, projectionMatrix);

	// Render the model.
	m_Model->Render(m_D3D->GetDevice());

	// Present the rendered scene to the screen.
	m_D3D->EndScene();

	return;
}